package com.sdgakj.service.feed;

import com.alibaba.fastjson.JSONObject;
import com.sdgakj.entity.Share;
import com.sdgakj.repository.ShareDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.Map;

/**
 * Created by 鲁俊杰 on 2017/4/18.
 * 家族历程业务逻辑层实现类
 */
@Component
@Transactional
public class ShareService {

    @Autowired
    private ShareDao shareDao;

    public JSONObject getShareByPage(Map<String, Object> searchParams,int pageNo, int pageSize, String sortType){
        JSONObject result=new JSONObject();
        Specification<Share> spec =buildSpecification(searchParams);
        PageRequest pageRequest = buildPageRequest(pageNo,pageSize,sortType);
        Page<Share> histories = shareDao.findAll(spec, pageRequest);

        result.put("data",histories);
        result.put("pageNo",pageNo);
        result.put("pageSize",pageSize);
        result.put("resMsg", "请求成功!");
        result.put("resCode", "1");
        return result;
    }

    public Page<Share> getSharesByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName,
                                      String sortType) {
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "fnCreatetime");
        //Specification<Truck> spec = buildSpecification(searchParams);
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Share> spec = DynamicSpecifications.bySearchFilter(filters.values(), Share.class);
        return shareDao.findAll(spec, pageRequest);
    }
    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
        Sort sort = null;
        if ("auto".equals(sortType)) {
            sort = new Sort(Sort.Direction.DESC, "id");
        }
        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }

    /**
     * 创建动态查询条件组合.
     */
    private Specification<Share> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        //  根据用户类型设置查询条件
        //	filters.put("user.type", new SearchFilter("user.type", Operator.EQ, EnumUserType.CUSTOMOR.getCode()));
        Specification<Share> spec = DynamicSpecifications.bySearchFilter(filters.values(),Share.class);
        return spec;
    }

    public JSONObject createShare(Share Share){
        JSONObject result=new JSONObject();
        try{
            shareDao.save(Share);
            result.put("resCode", "1");
            result.put("resMsg", "添加成功!");
        }catch (Exception e){
            e.printStackTrace();
            result.put("resCode", "0");
            result.put("resMsg", "添加失败!");
        }
        return result;
    }

    public Share getShareById(Long id){

        Share Share =shareDao.findOne(id);
        return Share;

    }
    public JSONObject updateShare(Share Share){
        JSONObject result=new JSONObject();
        shareDao.save(Share);
        result.put("resCode", "1");
        result.put("resMsg", "添加成功!");
        return result;
    }
    public JSONObject deleteShareById(Long id){
        JSONObject result=new JSONObject();
        shareDao.delete(id);
        result.put("resCode", "1");
        result.put("resMsg", "删除成功!");
        return result;
    }

}
